
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.IO;
using System.Text;
using com.ddsc.common;
using DDSC.OracleADO;
//using com.ddsc.database;
using com.ddsc.utility;



namespace smasb2b
{
	public class DataBase
	{
#region 呼叫後端method
        //Tony : 加入namesapce
		//新增
        public DataSet Addition(com.ddsc.common.ddscDataSet p_ddscDt, string tablename, Array TableNameF, Array TableNameT)
		{
			// 宣告回傳之dataset
			DataSet dsReturn = default(DataSet);
			// 宣告傳入之dataset
			DataSet ds = p_ddscDt.getDataSet();
			
			// create database access object
			dbAccess dba = new dbAccess();
			// 新增
			try
			{
				// 處理 client 端傳來的 DataSet 資料
				DataTable dt = ds.Tables[tablename];
				
				// 宣告轉SQL command物件 Sqlconverter
				SQLConverter sqlconvert = new SQLConverter();
				
				// 執行Sqlconverter，並傳入DataTable(前端傳入之ds中之issue_type Datatable)
				string[] sqlcommand = sqlconvert.convertToSQL(ds);
				int i = 0;
				int j = 0;
				if (!(TableNameF == null))
				{
					for (i = 0; i <= TableNameF.Length - 1; i++)
					{
						for (j = 0; j <= sqlcommand.Length - 1; j++)
						{
                            sqlcommand[j] = 
                                sqlcommand[j].Replace((string)TableNameF.GetValue(i), (string)TableNameT.GetValue(i));
						}
					}
				}
				if (!(sqlcommand == null))
				{
					dsReturn = dba.runExecuteNonQuery(sqlcommand);
				}
			}
			catch (Exception ex)
			{
				
				// create return object (DataSet)
				dsReturn = new DataSet(ds.DataSetName.ToString());
				dsReturn.Tables.Add(createErrorTable(ex.Message));
				
			}
			return dsReturn;
		}
		
		private DataTable createErrorTable(string mError)
		{
			
			dbReturnInfo dbInfo = new dbReturnInfo("info");
			dbInfo.AddNewRow();
			dbInfo.setReturnDataValue("F", System.Convert.ToString(999), 0, mError);
			return dbInfo.getReturnDataTable();
		}
		//建立者 : Titan Xei
		//函式說明 : 針對查詢資料的SQL語法查詢資料集
		//傳出值 : DataSet
		//傳入值 :  查詢語法, 暫存的表格名稱
        //Tony:新增傳入arrayList參數
		public DataSet Query_CLASS(string SelectCommandText, string DataTableName,ArrayList ArrParam)
        //public DataSet Query_CLASS(string SelectCommandText, string DataTableName)
		{
			DataSet dsReturn = default(DataSet);
			try
			{
				
				// DataTable Name
				string dtName = DataTableName;
				
				// create database access object
				dbAccess dba = new dbAccess();

                //Tony : 修改執行方法Overload
                //dsReturn = dba.runExecuteQuery(dtName, SelectCommandText);
                if (ArrParam.Count == 0) {
                    dsReturn = dba.runExecuteQuery(dtName, SelectCommandText);
                }
                else
                {
                    dsReturn = dba.runExecuteQuery(dtName, SelectCommandText, ArrParam);
                }
				
			}
			catch (Exception ex)
			{
				// create return object (DataSet)
				dsReturn = new DataSet("Query_CLASS");
				dsReturn.Tables.Add(createErrorTable(ex.Message));
			}
			return dsReturn;
			
		}
		

        //Tony:先做兩個假的殼
        public DataSet Query_CLASS(string SelectCommandText, string DataTableName)
        {
            return Query_CLASS(SelectCommandText , DataTableName , new ArrayList());
        }
        public DataSet Query_CLASS(dynamic sb)
        {
            return Query_CLASS(sb , new ArrayList());
        }

		public void clearSB(dynamic sb)
		{
			sb.Remove(0, sb.Length);
		}

        //Tony::新增傳入arrayList參數
		//public DataSet Query_CLASS(dynamic sb)
        public DataSet Query_CLASS(dynamic sb,ArrayList arrParam)
		{
			DataSet dsReturn = default(DataSet);
			
			try
			{
				
				// DataTable Name
				string dtName = "StringBuilder";
				
				// Sql command
				string sqlCommands = System.Convert.ToString(sb.ToString());
				
				// create database access object
				dbAccess dba = new dbAccess();

                //Tony : 修改執行方法Overload
				//dsReturn = dba.runExecuteQuery(dtName, sqlCommands);
                dsReturn = dba.runExecuteQuery(dtName, sqlCommands,arrParam);
				
			}
			catch (Exception ex)
			{
				// create return object (DataSet)
				dsReturn = new DataSet("Query_CLASS");
				dsReturn.Tables.Add(createErrorTable(ex.Message));
				
				
			}
			
			return dsReturn;
		}
		
		public dynamic GetDefaultTransactionNo(string CardID, int TransactionNoLength = 10)
		{
			int i = 0;
			string TransitionNo = CardID;
			string yy = DateTime.Today.ToString("yy");
			TransitionNo += yy;
            ArrayList m_arrParam = new ArrayList(); ;
            
            string SelectCommand = "select   " + CardID + ".nextval as SEQ from dual";

			DataBase DataBase = new DataBase();
			DataSet ds = default(DataSet);
            //Tony:改成呼叫新版本的方法
			ds = DataBase.Query_CLASS(SelectCommand, "dual",m_arrParam);
			int Row = 0;
			string no = "";
			string no_head = "";
			if (!(ds == null) && ds.Tables["info"].Rows[0]["ReturnCode"] != "100" && ds.Tables["info"].Rows[0]["ReturnCode"] != "999")
			{
				Row = System.Convert.ToInt32(ds.Tables["dual"].Rows.Count - 1);
				no = System.Convert.ToString(ds.Tables["dual"].Rows[Row]["seq"].ToString());
			}
			
			for (i = 1; i <= TransactionNoLength - TransitionNo.Length - no.Length; i++)
			{
				no_head = no_head + "0";
			}
			
			TransitionNo += no_head + no;
			return TransitionNo;
		}
		
		
#endregion
	}
	
}
